﻿Imports System.Data
Imports AutoDevModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_TiepNhanMCC_Update
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hidID.Value = Request("s")
            txtBienNhanNote.Text = hidID.Value
            ShowData()
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New AutoDevEntities
            Dim curHuyenId As Integer = Session("HuyenId")
            Dim p As HoSo = (From q In data.HoSoes Where q.SoBienNhan = hidID.Value And q.HuyenId = curHuyenId Select q).SingleOrDefault
            If Not p Is Nothing Then
                p.StatusId = 2 'xu ly xong
                If ddlAction.SelectedValue = 2 Then
                    p.IsUpdateToMCC = 1 ' tu choi , True: dong y
                Else
                    p.IsUpdateToMCC = 0 ' tu choi , True: dong y
                End If

                p.Ref1 = txtNoted.Text
                data.SaveChanges()
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/TiepNhan/MotCua.aspx';", Me.Page, True)
            Else
                Excute_Javascript("Alertbox('Không tồn tại biên nhận trong hệ thống.')", Me.Page, True)
            End If
        End Using

    End Sub

    Protected Sub ShowData()
        Using data As New AutoDevEntities
            Dim curHuyenId As Integer = Session("HuyenId")
            Dim p As HoSo = (From q In data.HoSoes Where q.SoBienNhan = hidID.Value And q.HuyenId = curHuyenId Select q).SingleOrDefault
            If Not p Is Nothing Then
                Using k As New VSModel.VSEntities
                    txtHuyenid.Text = (From h In k.Provinces Where h.Id = curHuyenId Select h.Title).FirstOrDefault
                End Using
                If Not p.IsUpdateToMCC Is Nothing Then
                    ddlAction.SelectedValue = p.IsUpdateToMCC

                End If
                txtNoted.Text = IIf(IsNothing(p.Ref1) = True, "", p.Ref1)
                txtSobiennhan.Text = IIf(IsNothing(p.SoBienNhan) = True, "", p.SoBienNhan)
                txtNgaytiepnhan.Text = IIf(IsNothing(p.NgayTiepNhan) = True, "", p.NgayTiepNhan)
                txtNgayhentra.Text = IIf(IsNothing(p.NgayHenTra) = True, "", p.NgayHenTra)
                txtNoidunghoso.Text = IIf(IsNothing(p.NoiDungHoSo) = True, "", p.NoiDungHoSo)
                txtTenhokinhdoanh.Text = IIf(IsNothing(p.TenHoKinhDoanh) = True, "", p.TenHoKinhDoanh)
                txtDiachikinhdoanh.Text = IIf(IsNothing(p.DiaChiKinhDoanh) = True, "", p.DiaChiKinhDoanh)
                txtNganhnghe.Text = IIf(IsNothing(p.NganhNghe) = True, "", p.NganhNghe)
                txtDienthoaidd.Text = IIf(IsNothing(p.DienThoaiDD) = True, "", p.DienThoaiDD)
                txtDienthoaiban.Text = IIf(IsNothing(p.DienThoaiBan) = True, "", p.DienThoaiBan)
                txtCmnd.Text = IIf(IsNothing(p.CMND) = True, "", p.CMND)
                txtNguoidaidien.Text = IIf(IsNothing(p.NguoiDaiDien) = True, "", p.NguoiDaiDien)
                txtDiachinguoidaidien.Text = IIf(IsNothing(p.DiaChiNguoiDaiDien) = True, "", p.DiaChiNguoiDaiDien)
                txtStatusid.Text = IIf(IsNothing(p.StatusId) = True, "", p.StatusId)
                txtMccurl.Text = IIf(IsNothing(p.MccURL) = True, "", p.MccURL)

                Select Case p.TypeId
                    Case "1"
                        txtTypeid.Text = "Chấm dứt"
                    Case "3"
                        txtTypeid.Text = "Cấp đổi"
                    Case "2"
                        txtTypeid.Text = "Cấp mới"
                    Case Else
                        txtTypeid.Text = "Cấp lại"

                End Select
                btnSave.Enabled = False
                Select Case p.StatusId
                    Case "1"
                        txtStatusid.Text = "Chờ xử lý"
                        btnSave.Enabled = True
                    Case "2"
                        txtStatusid.Text = "Chờ cập nhật MCC"
                    Case "3"
                        txtStatusid.Text = "Hoàn tất"
                    Case "4"
                        txtStatusid.Text = "Không hợp lệ - chờ cập nhật MCC"
                End Select
            End If
        End Using
    End Sub
End Class
